<template>
  <div class='container'>
    <el-form
      style="margin-bottom:50px"
      ref="formDataRef"
      :model="formData"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item label="文章标题" size="small" prop="title">
        <el-input v-model="formData.title"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" size="small" prop="articleBody">
        <quill-editor v-model="formData.articleBody"/>
      </el-form-item>
      <el-form-item label="视频地址" size="small">
        <el-input v-model="formData.videoURL"></el-input>
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="end">
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="hSubmit">确认</el-button>
    </el-row>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'

export default {
  props: {
    isEdit: {
      required: true,
      type: Boolean
    },
    backFormData: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      formData: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    if (this.isEdit) {
      this.formData = {
        ...this.backFormData
      }
    }
  },
  methods: {
   
    // 清空表单
    hClean () {
      this.formData = {
        subjectName: '',
        isFrontDisplay: ''
      }
      this.$refs.formDataRef.resetFields()
    },
    // 点击取消
    hCancel () {
      this.$emit('close')
    },
    // 点击提交
    hSubmit () {
      this.$refs.formDataRef.validate(async voild => {
        if (voild) {
          this.isEdit ? await this.doUpdate() : await this.doAdd()
          this.$emit('success')
        }
      })
    },

    // 做添加
    async doAdd () {
      try {
        const res = await add(this.formData)
        if (res.status === 200) {
          this.$message.success('添加成功')
        }
      } catch (error) {
        this.$message.error(error.message)
      }
    },

    // 做修改
    async doUpdate () {
      try {
        const res = await update({ ...this.formData })
        if (res.status === 200) {
          this.$message.success('修改成功')
        }
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
